package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.NotificationBean
import com.compass.mobile.models.PatientBean
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import stringListToJson
import toJson
import java.util.ArrayList

/**
 * Description： 用户信息veiwmodel
 * Date:2024/6/12 14:00
 */
class UserViewModel : BaseViewModel() {
    val messageListLiveData = MutableLiveData<MutableList<NotificationBean>?>()
    val patientLiveData = MutableLiveData<MutableList<PatientBean>?>()
    val patientBarLiveData = MutableLiveData<MutableList<PatientBean>?>()
    val refreshPatientLiveData = MutableLiveData<Boolean>()
    val addPatientLiveData = MutableLiveData<Boolean>()
    val modifyPatientLiveData = MutableLiveData<Boolean>()
    val addInformationLiveData = MutableLiveData<Boolean>()
    val hosFilmLiveData = MutableLiveData<String?>()
    val checkMobileLiveData = MutableLiveData<Boolean>()

    fun geMessage(patientId: String?, pageNo: Int, pageSize: Int = 100) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.geMessage(patientId, pageNo, pageSize, null)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                messageListLiveData.value = null
            } else {
                messageListLiveData.value = it.list
            }
        })
    }



    fun getHosConfig() {
        launchUIWithResult(responseBlock = {

            ApiManager.api.getHosConfig()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            hosFilmLiveData.value=it
        })
    }

    fun gePatients() {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.gePatients(1, 10)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            patientLiveData.value = it
        })
    }


    fun gePatientsForBar() {
        launchUIWithResult(responseBlock = {
            //1 是Android
            ApiManager.api.gePatients(1, 10)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            patientBarLiveData.value = it
        })
    }

    fun switchPatients(id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geSwitchPatients(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                refreshPatientLiveData.value = false
            }
        }, successBlock = {
            refreshPatientLiveData.value = true
        })
    }

    fun unbindPatients(id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geUnbindPatients(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                refreshPatientLiveData.value = false
            }
        }, successBlock = {
            refreshPatientLiveData.value = true
        })
    }

    /**
     * 添加就诊人
     */
    fun addPatients(name:String,idNumber:String,mobile: String,code:String,sex:Int,
                    age:String,bindType:String,guardianName:String,guardianCardNo: String,
                    guardianMobile: String,oldMobile:String?,oldCode:String?){

        val obj = JsonObject().apply {
            addProperty("idType","01")
            addProperty("idNumber",guardianCardNo)
            addProperty("mobile",guardianMobile)
            addProperty("name",guardianName)
        }

        val diseaseDescription = ArrayList<String>()
        diseaseDescription.add("")

        val params = JsonObject().apply {
            addProperty("name", name)
            addProperty("idType","01")
            addProperty("idNumber",idNumber)
            addProperty("code",code)
            addProperty("mobile",mobile)
            addProperty("sex",sex)
            addProperty("age",age)
            addProperty("idType","01")

            //部分默认参数
            addProperty("occupationCode","")
            addProperty("lat","")
            addProperty("lon","")
            addProperty("addrcodeCur","")
            addProperty("addrCur","")
            addProperty("addrCurDt","")

            addProperty("relationType","1")
            addProperty("pastHistoryStatus","0")
            addProperty("presentDiseaseStatus","0")
            addProperty("allergyHistoryStatus","0")
            addProperty("pregnancyHistoryStatus","0")
            addProperty("defaultStatus","0")
            addProperty("birthday",0)
            addProperty("bindType",bindType)

            addProperty("pastHistoryContent","")
            addProperty("presentDiseaseContent","")
            addProperty("allergyHistoryContent","")
//            add("diseaseDescription",diseaseDescription)
            add("diseaseDescription",stringListToJson(diseaseDescription))
            addProperty("avatar","")

            add("guardian",obj)

            if(bindType == "3"){
               //修改
                addProperty("oldMobile",oldMobile)
                addProperty("oldCode",oldCode)
            }else{
                addProperty("oldMobile","")
                addProperty("oldCode","")
            }
        }

        launchUIWithResult(responseBlock = {
           ApiManager.api.addPatients(params)
        },errorCall = object :IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
                if(code == 1104001001){
                    modifyPatientLiveData.value = true
                }else{
                    TipsToast.showTips(error)
                    addPatientLiveData.value = false
                }

            }
        }, successBlock = {
            addPatientLiveData.value = it!=null
        })
    }

    /**
     * 保存用户建议数据
     */
    fun saveInformationData(content:String,contactWay:String){
        launchUIWithResult(responseBlock = {
            ApiManager.api.saveInformation(content,contactWay)
        }, errorCall = object :IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                TipsToast.showTips(error)
                addInformationLiveData.value = false
            }

        }, successBlock = {
           if(it!=null){
               addInformationLiveData.value = true
           }
        })
    }

    fun getCode(mobile: String?) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            val params = JsonObject().apply {
                addProperty("mobile", mobile)
                addProperty("scene", "11")
            }
            ApiManager.api.getCode(params)
        }, errorCall = object : IApiErrorCallback {
        }, successBlock = {
            TipsToast.showTips("发送成功")

        })
    }

    /**
     * @param loginType //登录方式 1：自动，2：手动
     */
    fun getMobileSync(mobile: String?,smsCode:String?,loginType:String) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            val params = JsonObject().apply {
                addProperty("mobile", mobile)
                addProperty("smsCode", smsCode)
                addProperty("loginType", loginType)
            }
            ApiManager.api.getMobileSync(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                refreshPatientLiveData.value=false
            }
        }, successBlock = {

            refreshPatientLiveData.value=true
        })
    }

    /**
     * @param loginType //登录方式 1：自动，2：手动
     */
    fun checkMobile(mobile: String?,smsCode:String?,loginType:String) {
        launchUIWithResult(responseBlock = {
            //1 是Android
            val params = JsonObject().apply {
                addProperty("mobile", mobile)
                addProperty("smsCode", smsCode)
                addProperty("loginType", loginType)
            }
            ApiManager.api.checkMobileSync(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                checkMobileLiveData.value=false
            }
        }, successBlock = {
            if(it!=null){
                checkMobileLiveData.value= it
            }
        })
    }


}